package com.infore.dataReceiving.core;
import java.util.HashMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import com.infore.dataReceiving.core.parse.InstructionsParse;
import com.infore.dataReceiving.core.validation.InstructionsValidation;
import com.infore.dataReceiving.dto.AirInstructionsDto;
import com.infore.dataReceiving.util.Air212InstructionsHandleUtil;
import com.infore.dataReceiving.util.ResultEntity;

import io.netty.channel.ChannelHandlerContext;

@Component("instructions212Handle")
public class Instructions212Handle implements InstructionsHandle{
	
	private static final Logger log = LoggerFactory.getLogger(Instructions212Handle.class);  
	
	@Autowired 
	@Qualifier("air212InstructionsValidation")
	private InstructionsValidation instructionsValidation;
	
	@Autowired 
	@Qualifier("air212InstructionsParse")
	private InstructionsParse instructionsParse;
	
	@SuppressWarnings("unchecked")
	@Override
	public ResultEntity handle(String instruction,ChannelHandlerContext ctx) {
		//指令验证
		ResultEntity resultEntity = instructionsValidation.validation(instruction);
		if(resultEntity.getCode()!=4) {
			System.out.println(resultEntity.getMessage());
			log.error(resultEntity.getMessage()+instruction);  
			return resultEntity;
		}
		//指令解析
		AirInstructionsDto airInstructionsDto = (AirInstructionsDto)((HashMap<String, Object>)resultEntity.getContent()).get("info");
		if(Air212InstructionsHandleUtil.cMap.get("air_"+airInstructionsDto.getMn())==null) {
			Air212InstructionsHandleUtil.cMap.put("air_"+airInstructionsDto.getMn(), ctx);
			Air212InstructionsHandleUtil.channelMap.put(ctx.channel().id().asLongText(), airInstructionsDto.getMn());
		}
		resultEntity = instructionsParse.parse(airInstructionsDto, ctx);
		return resultEntity;
	}
	
	
	
	
	
	
	
	public static void main(String[] args) {
		Instructions212Handle handle = new Instructions212Handle();
		String msg = "##0127QN=20190523174006843;ST=21;CN=2061;PW=739404;MN=A430100_2007;Flag=9;CP=&&DataTime=20180507080000;w01001-Avg=7.1,w01001-Flag=N&&4F40\r\n";
		handle.handle(msg, null);
	}

}
